package com.kang.controller;

import java.util.Date;
import java.util.List;
import javax.annotation.Resource;
import javax.servlet.http.HttpSession;

import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.SessionAttributes;

import com.alibaba.fastjson.JSON;
import com.kang.pojo.AjaxResult;
import com.kang.pojo.Shop;
import com.kang.pojo.ShopItem;
import com.kang.service.IntemInformationService;
import com.kang.service.ShopItemService;
import com.kang.service.ShopService;
import com.vonechina.entity.page.ItemPage;
import com.vonechina.entity.page.ShopPage;

/**
 * 店仓管理控制层
 * 
 * @author zs614
 *
 */
@Controller
@RequestMapping("shop")
@SessionAttributes({ "shopPage" })
public class ShopController extends com.kang.controller.Controller {
	/**
	 * 店仓服务层
	 */
	@Resource
	private ShopService shopService;
	/**
	 * 物品服务层
	 */
	@Resource
	private IntemInformationService itemService;
	/**
	 * 店仓销售物品服务层
	 */
	@Resource
	private ShopItemService shopItemService;

	/**
	 * 分页查看店仓界面
	 * 
	 * @param session
	 * @param model
	 * @param shopPage
	 *            搜索条件自动封装
	 * @return 返回jsp页面
	 */
	@RequestMapping("listShop")
	public String listShop(HttpSession session, Model model, ShopPage shopPage) {
		initRight(session);
		if (checkRight("查看店仓") != SUCCESS) {
			return "basic/noRight";
		}
		model.addAttribute("isAdd", checkRight("新增店仓") == SUCCESS);
		model.addAttribute("isEdit", checkRight("编辑店仓") == SUCCESS);
		model.addAttribute("isDelete", checkRight("删除店仓") == SUCCESS);
		List<Shop> shopList = shopService.listShop(shopPage);
		model.addAttribute("shopList", shopList);
		shopPage.setPageUrl("shop/listShop.do");
		model.addAttribute("page", shopPage);
		return "basicData/shopManage/listShop";
	}

	/**
	 * 新增店仓
	 * 
	 * @param session
	 * @param shop
	 *            店仓实体类自动封装
	 * @return 返回json字符串
	 */
	@RequestMapping("add")
	@ResponseBody
	public String addshop(HttpSession session, Shop shop) {
		initRight(session);
		AjaxResult result = new AjaxResult();
		if (checkRight("新增店仓") != SUCCESS) {
			result.setResult("noRight");
			return JSON.toJSONString(result);
		}
		shop.setStorecode(shopService.generatoCode());
		shop.setCreatepersonid(userID);
		shop.setLastpersonid(userID);
		shop.setFinalprocessingtime(new Date());
		shop.setCreatetime(new Date());
		int a = shopService.insertSelective(shop);
		if (a > 0) {
			result.setResult("success");
		}
		return JSON.toJSONString(result);
	}

	/**
	 * 更新店仓信息
	 * 
	 * @param session
	 * @param itemIds
	 *            店仓可销售物品ID数组
	 * @param shop
	 *            店仓信息自动封装实体类
	 * @return 返回json字符串
	 */
	@RequestMapping("update")
	@ResponseBody
	public String updateshop(HttpSession session, Integer itemIds[], Shop shop) {
		initRight(session);
		AjaxResult result = new AjaxResult();
		if (checkRight("编辑店仓") != SUCCESS) {
			result.setResult("noRight");
			return JSON.toJSONString(result);
		}
		shopItemService.deleteShopItem(shop.getShopid(), itemIds);
		if (itemIds != null) {
			for (int i = 0; i < itemIds.length; i++) {
				ShopItem item = new ShopItem();
				item.setShopbusinessscopeid(shop.getShopid());
				item.setIteminformationid(itemIds[i]);
				if (shopItemService.selectShopItem(item) == null) {
					shopItemService.insert(item);
				}
			}
		}
		shop.setLastpersonid(userID);
		shop.setFinalprocessingtime(new Date());
		int a = shopService.updateByPrimaryKeySelective(shop);
		if (a > 0) {
			result.setResult("success");
		}
		return JSON.toJSONString(result);
	}

	/**
	 * 删除店仓
	 * 
	 * @param session
	 * @param shopid
	 *            店仓ID
	 * @return 返回json字符串
	 */
	@RequestMapping("delete")
	@ResponseBody
	public String deleteshop(HttpSession session, Integer shopid) {
		initRight(session);
		AjaxResult result = new AjaxResult();
		if (checkRight("删除店仓") != SUCCESS) {
			result.setResult("noRight");
			return JSON.toJSONString(result);
		}
		int a = shopService.deleteByPrimaryKey(shopid);
		if (a > 0) {
			result.setResult("success");
		}
		return JSON.toJSONString(result);
	}

	/**
	 * 前往更新店仓信息页面
	 * 
	 * @param session
	 * @param model
	 * @param shopid
	 *            店仓ID
	 * @return
	 */
	@RequestMapping("toUpdate")
	public String toUpdate(HttpSession session, Model model, Integer shopid) {
		initRight(session);
		model.addAttribute("isEdit", checkRight("编辑店仓") == SUCCESS);
		Shop shop = shopService.selectByPrimaryKey(shopid);
		List<ShopItem> itemList = shopItemService.listShopItem(shopid);
		model.addAttribute("itemList", itemList);
		model.addAttribute("shop", shop);
		return "basicData/shopManage/editShop";
	}

	/**
	 * 编辑店仓页面查询可添加物品信息
	 * 
	 * @param session
	 * @param model
	 * @param shopItemPage
	 *            查询条件自动封装
	 * @return 返回json字符串
	 */
	@RequestMapping("listItemforShop")
	@ResponseBody
	public String listItemforShop(HttpSession session, Model model, ItemPage shopItemPage) {
		List<ShopItem> iteminfolist = shopItemService.selectNotAddItem(shopItemPage);
		AjaxResult result = new AjaxResult();
		result.setRows(iteminfolist);
		result.setTotal(shopItemPage.getRows());
		return JSON.toJSONString(result);
	}
}
